package io.gitee.kotle.android.drawable

import android.graphics.*
import android.graphics.drawable.Drawable
import android.text.TextPaint
import io.gitee.kotle.android.utils.sp
import io.gitee.kotle.android.widget.helper.getTextCenterX
import io.gitee.kotle.android.widget.helper.getTextCenterY

/**
 *
 * 描述：
 *
 * 创建人：jiale.wei
 * 创建时间：2022/3/12 12:11 下午
 *
 */
open class CenterTextDrawable(
    val paint: TextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).also {
        it.style = Paint.Style.FILL
        it.textSize = 16f.sp
        it.color = Color.GRAY
    }
) : Drawable() {
    var text: String? = null

    /**
     * 文字大小
     */
    var textSize
        get() = paint.textSize
        set(value) {
            paint.textSize = value
        }

    /**
     * 文字颜色
     */
    var textColor
        get() = paint.color
        set(value) {
            paint.color = value
        }

    /**
     * 设置文字外观
     */
    var typeface
        get() = paint.typeface
        set(value) {
            paint.typeface = value
        }

    override fun draw(canvas: Canvas) {
        text?.let {
            val bounds=bounds
            val width = bounds.width().toFloat()
            val height = bounds.height().toFloat()
            canvas.drawText(
                it,
                paint.getTextCenterX(width, it),
                paint.getTextCenterY(height),
                paint
            )
        }
    }

    override fun setAlpha(p0: Int) {

    }

    override fun setColorFilter(p0: ColorFilter?) {
    }

    override fun getOpacity(): Int {
        return PixelFormat.TRANSPARENT
    }
}